package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import DB.DB_connection_pool;

public class My_attention_dynamic extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = response.getWriter();
		String user_account = request.getSession().getAttribute("user_account").toString();
		
		String attention_user = request.getParameter("attention_user");
		JSONObject jsonObj_r = new JSONObject();
		if(attention_user==null||attention_user.equals("")){       //请求所有关注
			
			String select_dynamic_id = "select dynamic_information_id from attention_dynamic where attention_user_id = '"+user_account+"'";
			String select_dynamic_data = "select * from dynamic_information where dynamic_information_id in ("+select_dynamic_id+")";
			
			JSONArray my_Array = new JSONArray();
			try {
				Connection conn = DB_connection_pool.data_pool.getConnection();
				PreparedStatement ps_select_dy = conn.prepareStatement(select_dynamic_data);
				ResultSet rs_dy = ps_select_dy.executeQuery();
				while(rs_dy.next()){
					JSONObject jsonObj = new JSONObject();
					String select_user_data = "select * from user_data where user_account = '"+rs_dy.getString("user_account")+"'";
					PreparedStatement ps_user = conn.prepareStatement(select_user_data);
					ResultSet rs_user = ps_user.executeQuery();
					if(rs_user.next()){
						jsonObj.put("user_account", rs_user.getString("user_account"));
						jsonObj.put("user_nickname", rs_user.getString("user_nickname"));                    //用户昵称
						jsonObj.put("user_photo_path", rs_user.getString("user_photo_path"));                //用户头像URL
					}
					rs_user.close();
					ps_user.close();
					jsonObj.put("dynamic_information_id", rs_dy.getString("dynamic_information_id"));
					jsonObj.put("dynamic_information_date", rs_dy.getString("dynamic_information_date"));
					jsonObj.put("dynamic_information_content", rs_dy.getString("dynamic_information_content"));
					jsonObj.put("dynamic_photo_path", rs_dy.getString("dynamic_photo_path"));
					jsonObj.put("dynamic_information_attention_number", rs_dy.getString("dynamic_information_attention_number"));
					jsonObj.put("dynamic_information_forword_number", rs_dy.getString("dynamic_information_forword_number"));
					jsonObj.put("dynamic_information_review_number", rs_dy.getString("dynamic_information_review_number"));
					jsonObj.put("dynamic_information_praise_number", rs_dy.getString("dynamic_information_praise_number"));
					
					
					String select_review = "select * from review where dynamic_information_id = '"+rs_dy.getString("dynamic_information_id")+"'";;
					PreparedStatement review_sel = conn.prepareStatement(select_review);
					ResultSet review_rs = review_sel.executeQuery();
					JSONArray review_Array = new JSONArray();
					while(review_rs.next()){
						JSONObject review_Obj = new JSONObject();
						
						String reviewer_account = review_rs.getString("reviewer_account");  //评论者账户
						String review_content = review_rs.getString("review_content"); //评论类容
						String review_date = review_rs.getDate("review_date").toString();  //评论时间
						String review_id = review_rs.getString("review_id");     //评论编号
						
						review_Obj.put("review_id", review_id);
						review_Obj.put("review_date", review_date);
						review_Obj.put("reviewer_account", reviewer_account);
						review_Obj.put("review_content", review_content);
						
						String reviewer_nickname = "";
						String reviewer_photo = "";
						String select_revewer_nick = "select * from user_data where user_account = '"+reviewer_account+"'";
						PreparedStatement reviewer_nic = conn.prepareStatement(select_revewer_nick);
						ResultSet reviewer_nic_rs = reviewer_nic.executeQuery();
						if(reviewer_nic_rs.next()){
							reviewer_nickname = reviewer_nic_rs.getString("user_nickname");
							reviewer_photo = reviewer_nic_rs.getString("user_photo_path");
						}
						review_Obj.put("reviewer_nickname", reviewer_nickname);
						review_Obj.put("reviewer_photo", reviewer_photo);
						
						review_Array.put(review_Obj);
						
						reviewer_nic.close();
						reviewer_nic_rs.close();
					}
					
					jsonObj.put("review_Array", review_Array);
					my_Array.put(jsonObj);
				}
				jsonObj_r.put("my_Array", my_Array);
				jsonObj_r.put("result", true);
				
				rs_dy.close();
				ps_select_dy.close();
				conn.close();
			} catch (SQLException e) {
				jsonObj_r.put("result", false);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			out.print(jsonObj_r.toString());
			
		}else{      //请求关注的某一个人
			System.out.println(attention_user);
		}
	}

}
